package com.javasm.bootcrud01.user.controller;

import com.javasm.bootcrud01.common.R;
import com.javasm.bootcrud01.exception.JavasmException;
import com.javasm.bootcrud01.exception.JavasmExceptionEnum;
import com.javasm.bootcrud01.user.entity.WebUser;
import com.javasm.bootcrud01.user.service.WebUserService;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("/user/info")
public class WebUserController {


    @Resource
   private WebUserService webUserService;

    @Resource
    RedisTemplate<String,Object> redisTemplate;

    @GetMapping("/query/{id}")
    public R queryById(@PathVariable Integer id){
        //先判断参数是否为空
        if(id == null){
            throw new JavasmException(JavasmExceptionEnum.UserNotExist);
        }
        String key = "user:uid" +id;
        Object o = redisTemplate.opsForValue().get(key);
        if (o != null){
            WebUser webUser = (WebUser) o;
            return R.ok(webUser);

        }
        WebUser webUser = webUserService.queryById(id);
        redisTemplate.opsForValue().set(key,webUser,1, TimeUnit.HOURS);
        return R.ok(webUser);

    }




    @GetMapping("/count")
    public  R  countUsers(@RequestParam Map<String, Object> params){

        Long count = webUserService.countUsers(params);
        System.out.println("params:"+params);
        return R.ok(count);
        //参数应该是Uid,额不是Id,这个方法是将数据库中不同的数据查询出来,希望能有个好结果


    }


    @GetMapping("/insert")
    public  R  insert(@RequestBody  WebUser webUser){
        webUserService.insert(webUser);
        return  R.ok(webUser);

    }


    @GetMapping("/update")
    public  R update(@RequestBody  WebUser webUser){
        webUserService.update(webUser);
        return R.ok(webUser);
    }

    @GetMapping("/delete")
    public  R  deleteBYUUID(@RequestParam Integer uid){
        WebUser webUser2 = webUserService.deleteByUid(uid);
        return R.ok(webUser2);
    }

    @GetMapping("/deletebyuids")
    public R  deleteByuuuids(@RequestParam List<Integer> uids){
        List<WebUser> webUsers = webUserService.deleteByIDsss(uids);
        System.out.println(webUsers);
        return R.ok(webUsers);
    }


    @GetMapping("/queryall")
    public R  queryByparams(@RequestBody WebUser webUser){
        List<WebUser> webUsers = webUserService.queryByparams(webUser);
        return  R.ok(webUsers);


    }


    @GetMapping("/insertAll")
    public  R  insertAll(@RequestBody List<WebUser>  webUserss){
        int i = webUserService.batchInsert(webUserss);
        return R.ok(i);
    }



 @PutMapping("/UpdateAll")
    public  R  batchUpdate(@RequestBody List<WebUser>  webUsers){
        int  i  = webUserService.batchUpdate(webUsers);
        return R.ok(i);
    }


    @GetMapping("/batchQuery")
    public R   batchQuery(@RequestParam List<Integer> Lists){
        List<WebUser> webUser = webUserService.batchQueryall(Lists);
        System.out.println(webUser);
        return R.ok(webUser);
    }



    @GetMapping("/page")
    public R listByPage (Integer pageNum,Integer pageSize){
        PageInfo<WebUser>  pageInfo  =  webUserService.listPage(pageNum,pageSize);
        return  R.ok(pageInfo);
    }

    @GetMapping("/user/{uid}")
    public  R getUser(@PathVariable  Integer  uid){

        WebUser webUser = webUserService.queryUserWithAccessLog(uid);
        return R.ok(webUser);
    }




    @GetMapping("/test/list")
    public  R  listwithRedis(){
        List<WebUser> webUsers = webUserService.listWithRedis();
        return  R.ok(webUsers);
    }

}

